<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="中标单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="biddingUnit">
              <SelectSupplierInput :disabled="formDisabled" placeholder="请选择中标单位" v-model="model.biddingUnit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="中标价(元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="bidding">
              <a-input-number :disabled="formDisabled" :max="99999999" :precision="2" v-model="model.bidding" placeholder="请输入中标价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="税率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxRate">
              <a-input
                type="number"
                :disabled="formDisabled"
                addon-after="%"
                placeholder="请输入税率"
                @blur="inputTaxRateBlur"
                v-model="model.taxRate" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="发票类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="invoiceCategory">
              <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.invoiceCategory" dictCode="invoice_type" placeholder="请选择发票类型" @blur="invoiceCategoryBlur"/>
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-model-item label="价格执行有效期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="validity">
              <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.validity" dictCode="validity_type" placeholder="请选择价格执行有效期" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="12">
            <a-form-model-item label="是否垫资" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isAdvancePayment">
              <j-dict-select-tag :disabled="formDisabled" type="list" v-model="model.isAdvancePayment" dictCode="yes_no" placeholder="请选择是否垫资"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工期(供货期)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="duration">
              <j-date
                :disabled="formDisabled"
                placeholder="请选择工期"
                v-model="model.duration"
                date-format="YYYY-MM-DD"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="付款方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payType">
              <a-textarea
                :disabled="formDisabled"
                v-model="model.payType"
                placeholder="请输入付款方式"
                :max-length="1000"
                rows="10"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import SelectSupplierInput from '@/views/scy/components/selectSupplier/SelectSupplierInput.vue'

  export default {
    name: 'ErpPurchaseCalibrationOptionUnitForm',
    components: {
      SelectSupplierInput
    },
    props: {
      // 表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        flag: false,
        model: {
          validity: null
        },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 }
        },
        confirmLoading: false,
        validatorRules: {
          biddingUnit: [
              { required: true, message: '请选择中标单位!', trigger: 'change' }
          ],
          bidding: [
              { required: true, message: '请填写中标价!', trigger: 'blur' },
              { pattern: /^([0-9]*[1-9][0-9]*(\.[0-9]+)?|[0]+\.[0-9]*[1-9][0-9]*)$/, message: '请输入大于0的数字!' }
          ],
          invoiceCategory: [
              { required: true, message: '请选择发票类型!', trigger: 'blur' }
          ],
          duration: [
            { required: true, message: '请选择工期!' }
          ],
          isAdvancePayment: [
            { required: true, message: '请选择是否垫资!' }
          ],
          payType: [
              { required: true, message: '请输入付款方式!', trigger: 'blur' }
          ],
          taxRate: [
              { required: true, message: '请填写税率!', trigger: 'blur' },
              { required: true, pattern: /^(([0-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的税率!' }
          ],
        },
      }
    },
    computed: {
      formDisabled() {
        return this.disabled
      }
    },
    created () {
       // 备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      // 税率输入框变化时
      inputTaxRateBlur (value) {
        if (!this.model.taxRate) {
          return
        }
        let item = value.srcElement._value.toString().slice(0, 2)
        let data = value.srcElement._value.toString().replace(/\b(0+)/gi, '0')
        if(item !== "0.") {
          // 去除0
          var num = data
        }
        if(data == ".") {
          this.model.taxRate = null
        }
        if (num >= 0 && num < 100) {
          this.model.taxRate = num
        }
        if (num < 0) {
          this.model.taxRate = 0
        }
        if (num >= 100) {
          this.model.taxRate = 100
        }
      },
      invoiceCategoryBlur (value) {
        this.$refs.form.clearValidate('taxRate');
        if(value == 0) {
          this.$set(this.model, 'taxRate', 0)
        } else {
          this.$set(this.model, 'taxRate', null)
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.$refs['form'].clearValidate();
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.$emit('ok', this.model, this.flag);
          }
        })
      }
    }
  }
</script>
<style scoped>
/* 在Chrome浏览器下 */
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}
/* 在Firefox浏览器下 */
input[type="number"]{
    -moz-appearance: textfield;
}
</style>
